package com.example.tao.mycar.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tao.mycar.R;

/**
 * Created by zhi-zhang on 2016/2/14.
 */
public class PicChooserDialog extends Dialog {

    public PicChooserDialog(Context context) {
        super(context);
    }

    public PicChooserDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected PicChooserDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String button1Text;
        private String button2Text;
        private String button3Text;
        private View contentView;
        private OnClickListener button1ClickListener;
        private OnClickListener button2ClickListener;
        private OnClickListener button3ClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Set the Dialog message from string
         *
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the button resource and it's listener
         *
         * @param Button1Text
         * @return
         */
        public Builder setButton1(int Button1Text, OnClickListener listener) {
            this.button1Text = (String) context.getText(Button1Text);
            this.button1ClickListener = listener;
            return this;
        }

        public Builder setButton1(String Button1Text, OnClickListener listener) {
            this.button1Text = Button1Text;
            this.button1ClickListener = listener;
            return this;
        }

        public Builder setButton2(int Button2Text, OnClickListener listener) {
            this.button1Text = (String) context.getText(Button2Text);
            this.button2ClickListener = listener;
            return this;
        }

        public Builder setButton2(String Button2Text, OnClickListener listener) {
            this.button2Text = Button2Text;
            this.button2ClickListener = listener;
            return this;
        }

        public Builder setButton3(int Button3Text, OnClickListener listener) {
            this.button1Text = (String) context.getText(Button3Text);
            this.button3ClickListener = listener;
            return this;
        }

        public Builder setButton3(String Button3Text, OnClickListener listener) {
            this.button3Text = Button3Text;
            this.button3ClickListener = listener;
            return this;
        }



        public PicChooserDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final PicChooserDialog dialog = new PicChooserDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_pic_chooser, null);
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            dialog.addContentView(layout, lp);
            setWindowSize(dialog);
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);

            if (button1Text != null) {
                ((Button) layout.findViewById(R.id.pic_chooser_camera)).setText(button1Text);
                if (button1ClickListener != null) {
                    layout.findViewById(R.id.positiveButton).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            button1ClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.pic_chooser_camera).setVisibility(View.GONE);
            }

            if (button2Text != null) {
                ((Button) layout.findViewById(R.id.pic_chooser_gallery)).setText(button2Text);
                if (button2ClickListener != null) {
                    layout.findViewById(R.id.pic_chooser_gallery).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            button2ClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.pic_chooser_camera).setVisibility(View.GONE);
            }

            if (button3Text != null) {
                ((Button) layout.findViewById(R.id.pic_chooser_cancel)).setText(button3Text);
                if (button3ClickListener != null) {
                    layout.findViewById(R.id.pic_chooser_cancel).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            button3ClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.pic_chooser_camera).setVisibility(View.GONE);
            }

            dialog.setContentView(layout);
            return dialog;
        }

        private void setWindowSize(Dialog dialog) {
            DisplayMetrics dm = new DisplayMetrics();
            Window dialogWindow = dialog.getWindow();
            WindowManager m = dialogWindow.getWindowManager();
            m.getDefaultDisplay().getMetrics(dm);
            // 为获取屏幕宽、高
            WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//            p.height = (int) (dm.heightPixels * 0.25); //高度设置为屏幕的0.25
            p.width = (int) (dm.widthPixels * 1.0); // 宽度设置为屏幕的1.0
            p.alpha = 1.0f; // 设置本身透明度
            p.dimAmount = 0.6f; // 设置黑暗度
            dialogWindow.setAttributes(p);
        }
    }
}
